home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / ex / ex13-7.c < prev    next >
C/C++ Source or Header  |  1990-05-15  |  791b  |  41 lines

  1. // ex13-7.c -- Calling a virtual function from a
  2. //             base class constructor
  3.  
  4. // $Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/ex/RCS/ex13-7.c,v 3.0 90/05/15 22:44:47 kgorlen Rel $
  5.  
  6. #include <iostream.h>
  7.  
  8. class V {
  9. public:
  10.     virtual void vf();
  11. };
  12.  
  13. void V::vf()    { cout << "V::vf()" << endl; }
  14.  
  15. class A: public virtual V {
  16. public:
  17.     A()         { /* ... */ }
  18.     virtual void vf();
  19. };
  20.  
  21. void A::vf()    { cout << "A::vf()" << endl; }
  22.  
  23. class B: public virtual V {
  24. public:
  25.     B()         { vf(); }   // Calls A::vf(), not
  26.                             // V::vf() or C::vf()
  27. };
  28.  
  29. class C: public A, public B {
  30. public:
  31.     C()         { vf(); }   // Calls C::vf()
  32.     virtual void vf();
  33. };
  34.  
  35. void C::vf()    { cout << "C::vf()" << endl; }
  36.  
  37. main()
  38. {
  39.     C c;
  40. }
  41.